package com.ai3d.rfid.common.libs

import android.app.Activity
import android.os.Build
import android.os.Bundle
import com.ai3d.rfid.base.app.App
import com.ai3d.rfid.common.log.Logcat
import com.ai3d.rfid.common.proxy.MMKVProxy
import com.ai3d.rfid.common.proxy.MMKVProxy.KEY_SETTINGS_LANGUAGE_MAIN
import com.bj.disc.ui.login.activity.LoginActivity
import com.bj.disc.util.DiscConstants.HEADER_CONTENT_LANGUAGE_CANTONESE
import com.bj.disc.util.DiscConstants.HEADER_CONTENT_LANGUAGE_EN_US
import com.bj.disc.util.DiscConstants.HEADER_CONTENT_LANGUAGE_HOKKIEN
import com.bj.disc.util.DiscConstants.HEADER_CONTENT_LANGUAGE_ZH_CN
import java.util.*

/**
 * @ProjectName : rfid
 * @Author : weijinsong
 * @Time : 2023/6/7 19:33
 * @Description : 文件描述
 */
object InitLanguage : ActivityLifecycle() {

    private const val TAG = "InitLanguage"
    var locale: Locale = Locale.SIMPLIFIED_CHINESE
    override fun init(app: App) {
        locale = getLanguage(getLanguageCache())
        super.init(app)
//        appViewModel.getLanguage()
    }

    override fun onActivityCreated(activity: Activity, p1: Bundle?) {
        val resources = activity.resources
        val configuration = resources.configuration
        // app locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale)
        } else {
            configuration.locale = locale
        }
        resources.updateConfiguration(configuration, resources.displayMetrics)
    }

    /**
     * 将服务器语言转换成android可用的语言
     */
    private fun getLanguage(language: String): Locale {
        return when (language) {
            HEADER_CONTENT_LANGUAGE_ZH_CN,
            HEADER_CONTENT_LANGUAGE_HOKKIEN,
            HEADER_CONTENT_LANGUAGE_CANTONESE -> Locale.SIMPLIFIED_CHINESE
            HEADER_CONTENT_LANGUAGE_EN_US -> Locale.ENGLISH
            else -> Locale.SIMPLIFIED_CHINESE
        }
    }

    fun setAppLanguage(tempLanguage: String, inputAccount: String?, inputPassWord: String?) {
        if (setLanguageCache(tempLanguage) && App.instance != null) {

            for (activity in activityList) {
                //会闪一下
//                if (activity is SettingActivity) {
//                    continue
//                }
                val resources = activity.resources
                if (resources != null) {
                    val configuration = resources.configuration
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        configuration.setLocale(locale)
                    } else {
                        configuration.locale = locale
                    }
                    resources.updateConfiguration(configuration, resources.displayMetrics)
                    if (activity is LoginActivity) {
                        activity?.let { it1 -> LoginActivity.startLoginActivity(it1, inputAccount, inputPassWord) }
                    } else {
                        activity.recreate()
                    }
                    Logcat.d(
                        TAG,
                        "setAppLanguage tempFontStr : $tempLanguage  locale : ${locale}"
                    )
                }
            }
        }
    }

    private fun setLanguageCache(tempLanguage: String): Boolean {
        var fontStr = getLanguageCache()
        if (fontStr == tempLanguage
            || (fontStr == HEADER_CONTENT_LANGUAGE_ZH_CN && (tempLanguage == HEADER_CONTENT_LANGUAGE_HOKKIEN || tempLanguage == HEADER_CONTENT_LANGUAGE_CANTONESE))
            || (fontStr == HEADER_CONTENT_LANGUAGE_HOKKIEN && (tempLanguage == HEADER_CONTENT_LANGUAGE_ZH_CN || tempLanguage == HEADER_CONTENT_LANGUAGE_CANTONESE))
            || (fontStr == HEADER_CONTENT_LANGUAGE_CANTONESE && (tempLanguage == HEADER_CONTENT_LANGUAGE_ZH_CN || tempLanguage == HEADER_CONTENT_LANGUAGE_HOKKIEN))) {
            MMKVProxy.setString(KEY_SETTINGS_LANGUAGE_MAIN, tempLanguage)
            return false
        }
        MMKVProxy.setString(KEY_SETTINGS_LANGUAGE_MAIN, tempLanguage)
        locale = getLanguage(tempLanguage)
        return true
    }

    private fun getLanguageCache(): String {
        return MMKVProxy.getString(
            KEY_SETTINGS_LANGUAGE_MAIN, HEADER_CONTENT_LANGUAGE_EN_US
        )
    }

    fun finishAll() {
//        for (activity in activityList) {
//            if (!activity.isFinishing) {
//                activity.finish()
//            }
//        }
        while (activityList.size > 0){
            var activity = activityList[0]
            activityList.removeAt(0)
            if (!activity.isFinishing) {
                activity.finish()
            }
        }
        activityList.clear()
    }
}